package com.ishopping.web.manage.controller;


import com.ishopping.common.core.base.constant.CommonConstants;
import com.ishopping.common.core.base.controller.BaseController;
import com.ishopping.api.manage.entity.Organization;
import com.ishopping.api.manage.service.IOrganizationService;
import com.ishopping.common.core.base.enums.ReturnEnum;
import com.ishopping.common.core.vo.JsonResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 机构controller
 */
@Controller
@RequestMapping("/system/organization")
public class OrganizationController extends BaseController {
    @Autowired
    private IOrganizationService organizationService;

    /**
     * 机构管理页面
     */
    @RequiresPermissions("system:organization:manage")
    @RequestMapping(value="/manage", method = RequestMethod.GET)
    public String organizationManage(){
        return "system/organizationMgt";
    }

    /**
     * 机构列表
     */
    @RequiresPermissions("system:organization:manage")
    @RequestMapping(value="/list", method = RequestMethod.GET)
    @ResponseBody
    public List<Organization> organizationList(){
        List<Organization> organizationList = organizationService.queryAll();
        return organizationList;
    }

    /**
     * 添加机构跳转
     * @param model
     */
    @RequiresPermissions("system:organization:add")
    @RequestMapping(value = "/addForm", method = RequestMethod.GET)
    public String addOrganizationForm(Model model) {
        model.addAttribute("organization", new Organization());
        model.addAttribute("action", CommonConstants.ADD);
        return "system/organizationForm";
    }

    /**
     * 添加机构
     * @param organization
     */
    @RequiresPermissions("system:organization:add")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Object addOrganization(Organization organization) {
        int result = organizationService.save(organization);
        if(result > 0) {
            return JsonResult.success();
        }else{
            return JsonResult.build(ReturnEnum.USER_FAIL.getValue(), ReturnEnum.USER_FAIL.getText());
        }
    }

    /**
     * 修改机构跳转
     * @param id
     * @param model
     * @return
     */
    @RequiresPermissions("system:organization:update")
    @RequestMapping(value = "/updateForm/{id}", method = RequestMethod.GET)
    public String updateOrganizationForm(@PathVariable("id") Long id, Model model) {
        model.addAttribute("organization", organizationService.queryById(id));
        model.addAttribute("action", CommonConstants.UPDATE);
        return "system/organizationForm";
    }

    /**
     * 修改机构
     * @param organization
     * @return
     */
    @RequiresPermissions("system:organization:update")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Object updateOrganization(Organization organization) {
        int result = organizationService.updateSelective(organization);
        if(result > 0) {
            return JsonResult.success();
        }else{
            return JsonResult.build(ReturnEnum.USER_FAIL.getValue(), ReturnEnum.USER_FAIL.getText());
        }
    }

    /**
     * 删除机构
     * @param id
     * @return
     */
    @RequiresPermissions("system:organization:delete")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Object deleteOrganization(@PathVariable("id") Long id) {
        int result = organizationService.deleteById(id);
        if(result > 0) {
            return JsonResult.success();
        }else{
            return JsonResult.build(ReturnEnum.USER_FAIL.getValue(), ReturnEnum.USER_FAIL.getText());
        }
    }

}
